package com.example.indenture.company.service.impl;

import com.example.indenture.common.vo.Result;
import com.example.indenture.company.dao.ArticleDao;
import com.example.indenture.company.entity.Article;
import com.example.indenture.company.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: wyt
 * @Date: 2023/6/10 18:12
 **/
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
     private ArticleDao articleDao;

    @Override
    @Transactional
    public Integer addArticle(Article article) {
        if (article == null) {
            throw new IllegalArgumentException("Article cannot be null!");
        }
        article.setCreateDate(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        return articleDao.insertArticle(article);
    }

    @Override
    @Transactional
    public Integer deleteArticleByCidAndRid(Integer cid, Integer rid) {
        if (cid == null || rid ==null) {
            Result.failed("id cannot be null!");
        }
        return articleDao.deleteArticleByCidAndRid(cid, rid);
    }

    @Override
    @Transactional
    public List<Article> getArticleByCidAndRid(Integer cid, Integer rid) {
        final List<Article> articles = articleDao.getArticleByCidAndRid(cid, rid);
        return articles;
    }
}
